package com.fita.admin.service.report.impl;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.persistence.GenericDaoSupport;
import com.fita.admin.service.DataServiceImpl;
import com.fita.admin.service.report.WithdrawRecordService;
import com.fita.entity.order.OrderPayType;
import com.fita.entity.wallet.WithdrawRecord;
import com.fita.entity.wallet.WithdrawRecordPaidStatus;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 */
@Service("withdrawRecordService")
public class WithdrawRecordServiceImpl extends DataServiceImpl<WithdrawRecord> implements WithdrawRecordService {

    @Autowired
    private GenericDaoSupport genericDaoSupport;

    /**
     * 提现同意
     *
     * @param withdrawRecordId
     * @param payType
     * @param payCode
     */
    public void confirm(Long withdrawRecordId, OrderPayType payType, String payCode) {

        WithdrawRecord withdrawRecord = genericDaoSupport.load(WithdrawRecord.class, withdrawRecordId);

        if (withdrawRecord == null){
            throw BusinessException.newInstance("500", "该记录不存在");
        }
        if (withdrawRecord.getPaidStatus() != WithdrawRecordPaidStatus.UNPAID || StringUtils.isNotBlank(withdrawRecord.getPayCode())){
            throw BusinessException.newInstance("500", "该提现请求已同意");
        }
        withdrawRecord.confirm(payType, payCode);

        genericDaoSupport.update(withdrawRecord);
    }
}
